package com.xiaoshi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshi.exception.PlatformException;
import com.xiaoshi.system.dto.SysDictTypeDto;
import com.xiaoshi.system.entity.SysDictData;
import com.xiaoshi.system.entity.SysDictType;
import com.xiaoshi.system.mapper.SysDictTypeMapper;
import com.xiaoshi.system.qo.SysDictTypeQo;
import com.xiaoshi.system.service.ISysDictDataService;
import com.xiaoshi.system.service.ISysDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author yuankai
 * @since 2022-03-24
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {
    @Autowired
    private ISysDictDataService sysDictDataService;

    @Override
    public IPage getPage(SysDictTypeQo qo) {
        QueryWrapper qw = new QueryWrapper();
        if(StrUtil.isNotEmpty(qo.getDictType())){
            qw.eq(SysDictType.DICT_TYPE,qo.getDictType());
        }
        if(StrUtil.isNotEmpty(qo.getDictName())){
            qw.eq(SysDictType.DICT_NAME,qo.getDictName());
        }
        if(StrUtil.isNotEmpty(qo.getCreateStartTime())){
            qw.ge(SysDictType.CREATE_TIME,qo.getCreateStartTime());
        }
        if(StrUtil.isNotEmpty(qo.getCreateEndTime())){
            qw.le(SysDictType.CREATE_TIME,qo.getCreateEndTime());
        }
        qw.eq(SysDictType.DEL_TAG,0);
        IPage<SysDictType> page = this.page(new Page<>(qo.getCurrent(),qo.getSize()),qw);

        return page;
    }

    @Override
    public boolean addSysDictType(SysDictTypeDto sysDictDto) {

        SysDictType byName = getByName(sysDictDto.getDictName());
        if(ObjectUtil.isNotNull(byName)){
            throw new PlatformException("该名称已存在！");
        }
        SysDictType byType = getByType(sysDictDto.getDictType());
        if(ObjectUtil.isNotNull(byType)){
            throw new PlatformException("该类型已存在！");
        }
        SysDictType sysDictType = new SysDictType();
        BeanUtil.copyProperties(sysDictDto,sysDictType);

        return this.save(sysDictType);
    }

    @Override
    public SysDictType getByName(String name) {
        QueryWrapper qw = new QueryWrapper();
        if(StrUtil.isNotEmpty(name)){
            qw.eq(SysDictType.DICT_NAME,name);
        }
        return getOne(qw);
    }

    public SysDictType getByType(String type) {
        QueryWrapper qw = new QueryWrapper();
        if(StrUtil.isNotEmpty(type)){
            qw.eq(SysDictType.DICT_TYPE,type);
        }
        return getOne(qw);
    }

    @Override
    public boolean editSysDictType(SysDictTypeDto sysDictTypeDto) {
        SysDictType byNameAndId = getByNameAndId(sysDictTypeDto.getDictName(), sysDictTypeDto.getDictId());
        if(ObjectUtil.isNotNull(byNameAndId)){
            throw new PlatformException("该名称已存在！");
        }
        SysDictType sysDictType  = new SysDictType();
        QueryWrapper qw = new QueryWrapper();
        qw.eq(SysDictData.DICT_TYPE,sysDictTypeDto.getDictType());
        qw.eq(SysDictData.DEL_TAG,0);
        List<SysDictData> list = sysDictDataService.list(qw);
        if(!CollectionUtils.isEmpty(list)){
            throw new PlatformException("使用字典，不能修改！");
        }

        BeanUtil.copyProperties(sysDictTypeDto,sysDictType);
        boolean b = this.updateById(sysDictType);
        if(b){
            return sysDictDataService.updateType(sysDictType.getDictType());
        }
        return b;
    }

    @Override
    public boolean delById(Long id) {
        // 检查子项，没有在删除
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper();
        queryWrapper.eq(SysDictData.ID,id);
        List<SysDictData> dictDatas = sysDictDataService.list(queryWrapper);
        if(!CollectionUtils.isEmpty(dictDatas)){
            throw new PlatformException("存在子项，删除失败！");
        }

        LambdaUpdateWrapper<SysDictType> qw = new LambdaUpdateWrapper();
        qw.eq(SysDictType::getDictId,id).set(SysDictType::getDictType,1);

        return this.update(qw);
    }

    public SysDictType getByNameAndId(String name,Long id) {
        QueryWrapper qw = new QueryWrapper();
        if(StrUtil.isNotEmpty(name)){
            qw.eq(SysDictType.DICT_NAME,name);
        }
        if(null != id){
            qw.ne(SysDictType.DICT_ID,id);
        }
        return getOne(qw);
    }
}
